#!/bin/bash
# generate a .pkg file
# Format reference: "Stata [R] => net => Format of content and package-description files"
# (pg 12 of http://www.stata.com/manuals13/rnet.pdf)

cd $(dirname $1)
PKG=$(basename $1 .pkg)
DESCRIPTION=$2
AUTHOR=$3

(
# state the .pkg version number
echo v 3

# package description
echo d $PKG
echo d $DESCRIPTION
echo d $AUTHOR
# "including Distribution-Date in that format enables 'adoupdate'"
echo d Distribution-Date: `date +%Y%m%d`
echo

# package contenst
#
# The folder structure in the package is ignored: files go "into the adopath",
# which means everything starting with 'a' goes under %ADOPATH%/a/, everything starting 'b' goes under %ADOPATH%/b/, etc.
# 
# F means a regular file 
# G means a platform-specific file: this is only installed if the platform given on the line matches
# h doesn't declare a file to be installed; rather it asserts that a particular
#  file should have been installed by one of the F or G lines
#
# So what we do is blindly index all the files (find -type f) in the folder as Fs
# except that bin/ becomes G files, tagged with the platform that's in the subfolder of bin/
# e.g. bin/MACINTEL64/file is written as 'g MACINTEL64 $PKG/bin/MACINTEL64/file
# and also any .plugin files found under bin/ are duplicated to 'h file.plugin'
# because the idea is that we have three .plugin files and at install time one of
# them is chosen to be put on the adopath.
#
# Finally, capitalization (F vs f, or G vs g) controls if a file is "ancillary";
# Files with a capitalized tag are installed to the adopath at "net install"
#  time: these are part of the core package;
# Lower-case files are considered ancillary, only get downloaded on "net get",
#  and end up in the current directory: these are meant for datasets and examples.

if [ -d "$PKG" ]; then
  find "$PKG" \( -name "bin" -prune -o -name "ancillary" -prune -o -true \) -type f | awk '{ print "F " $1 }'
  if [ -d "$PKG/bin" ]; then
    find "$PKG/bin" -mindepth 2 -maxdepth 3 -type f  | awk '{ split($1,A,"/"); print "G " A[3] " " $1 }'
    find "$PKG/bin" -type f -name "*.plugin" -exec basename {} \; | sort | uniq | awk '{ print "h " $1 }'
  fi
  if [ -d "$PKG/ancillary" ]; then
    find "$PKG/ancillary" \( -name "bin" -prune -o -true \) -type f | awk '{ print "f " $1 }'
  fi
fi
) > $PKG.pkg
